#include <stdio.h>
int main()
{
    int m;
    scanf("%d",&m);
    while (m--)
    {
        int n,i,sum,j;
        scanf("%d",&n);
        int a[n],ave;
        for (i=0;i<n;i++)scanf("%d",&a[i]);
        for (i=0;i<n-1;i++)
            for (j=i+1;j<n;j++)
                if (a[i]>a[j])
                {
                    int temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
        ave=a[n/2];sum=0;
        for (i=0;i<n;i++)
        {
            if (a[i]-ave<0)sum+=ave-a[i];
            else sum+=a[i]-ave;
        }
        printf("%d\n",sum);
    }
    return 0;
}
